import pygame
import sys
import math
import random
from pygame import gfxdraw

# 初始化pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("粉色渐变汇聚爱心")

# 颜色定义（粉色渐变系列）
PINK_COLORS = [
    (255, 183, 197),  # 浅粉
    (255, 148, 167),
    (255, 113, 137),
    (255, 78, 107),
    (255, 43, 77),   # 深粉
]

# 爱心点类
class HeartPoint:
    def __init__(self):
        # 随机初始位置（屏幕四周）
        edge = random.choice(['top', 'bottom', 'left', 'right'])
        if edge == 'top':
            self.x = random.randint(0, WIDTH)
            self.y = -50
        elif edge == 'bottom':
            self.x = random.randint(0, WIDTH)
            self.y = HEIGHT + 50
        elif edge == 'left':
            self.x = -50
            self.y = random.randint(0, HEIGHT)
        else:  # right
            self.x = WIDTH + 50
            self.y = random.randint(0, HEIGHT)
            
        # 随机大小
        self.size = random.randint(2, 6)
        # 随机粉色
        self.color = random.choice(PINK_COLORS)
        # 目标位置（爱心形状上的点）
        self.target_angle = random.uniform(0, 2 * math.pi)
        self.target_radius = random.uniform(0.5, 1.0)
        self.speed = random.uniform(1, 4)
        self.arrived = False

    def update(self, center_x, center_y, scale):
        if self.arrived:
            return
            
        # 计算目标点（基于爱心参数方程）
        t = self.target_angle
        x = 16 * (math.sin(t) ** 3)
        y = -(13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t))
        
        # 缩放并定位到中心
        target_x = center_x + x * scale * self.target_radius
        target_y = center_y + y * scale * self.target_radius
        
        # 移动到目标点
        dx = target_x - self.x
        dy = target_y - self.y
        distance = math.sqrt(dx**2 + dy**2)
        
        if distance < self.speed:
            self.x = target_x
            self.y = target_y
            self.arrived = True
        else:
            self.x += dx / distance * self.speed
            self.y += dy / distance * self.speed

    def draw(self, surface):
        gfxdraw.filled_circle(
            surface, 
            int(self.x), 
            int(self.y), 
            self.size, 
            self.color
        )

def main():
    clock = pygame.time.Clock()
    center_x, center_y = WIDTH // 2, HEIGHT // 2
    scale = 10  # 爱心大小比例
    points = [HeartPoint() for _ in range(800)]  # 爱心点数量
    all_arrived = False
    animation_step = 0

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # 填充背景
        screen.fill((255, 255, 255))  # 白色背景

        if not all_arrived:
            # 更新所有点
            animation_step += 1
            current_scale = min(scale, scale * (animation_step / 100))  # 逐渐增大爱心
            arrived_count = 0
            
            for point in points:
                point.update(center_x, center_y, current_scale)
                if point.arrived:
                    arrived_count += 1
                point.draw(screen)
            
            # 检查是否所有点都已到达目标位置
            if arrived_count == len(points):
                all_arrived = True
        else:
            # 所有点到达后，保持爱心形状并轻微脉动
            pulse = (math.sin(pygame.time.get_ticks() / 500) + 1) / 2 * 0.1 + 0.9
            for point in points:
                # 轻微脉动效果
                t = point.target_angle
                x = 16 * (math.sin(t) ** 3)
                y = -(13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t))
                
                point.x = center_x + x * scale * point.target_radius * pulse
                point.y = center_y + y * scale * point.target_radius * pulse
                point.draw(screen)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()
